package com.iap.client.mediator
{
	import com.iap.app.AppGlobal;
	import com.iap.client.view.instance.InstanceEditWindow;
	import com.iap.client.view.instance.InstanceListWindow;
	import com.iap.common.AppSession;
	import com.iap.common.event.ClientEvent;
	import com.iap.common.event.ClientLiteEvent;
	import com.iap.common.event.InstanceEvent;
	import com.iap.common.mediator.BaseMediator;
	import com.iap.common.model.Client;
	import com.iap.common.model.ClientInstance;
	import com.iap.common.model.ClientLite;
	import com.iap.common.model.ClientLiteInstanceLite;
	import com.iap.common.model.Instance;
	import com.iap.common.model.InstanceLite;
	import com.iap.common.model.Report;
	import com.iap.common.signal.ClientLiteRequestSignal;
	import com.iap.common.signal.ClientRequestSignal;
	import com.iap.common.signal.InstanceRequestSignal;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import spark.events.GridEvent;
	
	public class InstanceListWindowMediator extends BaseMediator
	{
		[Inject]
		public var view:InstanceListWindow;
		[Inject]
		public var clientRequest:ClientLiteRequestSignal;
		
		private var instanceEditWindow:InstanceEditWindow;
		
		override public function onRegister():void
		{
			view.grid.addEventListener(GridEvent.GRID_CLICK, instanceGridClickHandler);
			view.addButton.addEventListener(MouseEvent.CLICK, addClickHandler);
			addContextListener(InstanceEvent.RESPONSE, instanceResponseHandler);
			addContextListener(ClientLiteEvent.RESPONSE, clientLiteResponseHandler);
		}
		
		private function clientLiteResponseHandler(evt:ClientLiteEvent):void
		{
			AppGlobal.endWait();
			if(evt.response.task == ClientLiteRequestSignal.GET)
			{
				var item:ClientLite = evt.response.result as ClientLite;
				view.instanceData = new ArrayCollection(item.instanceLites);
			}
		}

		private function instanceResponseHandler(evt:InstanceEvent):void
		{
			AppGlobal.endWait();
			if(evt.response.task == InstanceRequestSignal.GET)
			{
				view.instanceData = new ArrayCollection(evt.response.result as Array);
			}
		}
		
		private function instanceGridClickHandler(evt:GridEvent):void
		{
			var item:ClientLiteInstanceLite = evt.item as ClientLiteInstanceLite;
			if(item == null) return;
			
			var clientInstance:ClientInstance = new ClientInstance();
			clientInstance.id = item.id;
			clientInstance.clientId = item.clientId;
			
			var instanceLite:InstanceLite = item.instance;
			var instance:Instance = new Instance();
			instance.id = instanceLite.id;
			instance.name = instanceLite.name;
			clientInstance.instance = instance;

			openInstanceEdit(clientInstance);
		}
		
		private function addClickHandler(evt:MouseEvent):void
		{
			var item:ClientInstance = new ClientInstance();
			item.clientId = AppSession.currentClient.id;
			item.instance = new Instance();
			item.instance.report = new Report();
			openInstanceEdit(item);
		}
		
		private function openInstanceEdit(item:ClientInstance):void
		{
			instanceEditWindow = new InstanceEditWindow();
			instanceEditWindow.model = item;
			PopUpManager.addPopUp(instanceEditWindow, view.root, true);
			PopUpManager.centerPopUp(instanceEditWindow);
			instanceEditWindow.addEventListener(CloseEvent.CLOSE, instanceEditCloseHandler);
			view.root.addEventListener(Event.RESIZE, instanceEditResizeHandler);
		}
		
		private function instanceEditResizeHandler(evt:Event):void
		{
			PopUpManager.centerPopUp(instanceEditWindow);
		}
		
		private function instanceEditCloseHandler(evt:Event):void
		{
			view.root.removeEventListener(Event.RESIZE, instanceEditResizeHandler);
			instanceEditWindow.removeEventListener(CloseEvent.CLOSE, instanceEditCloseHandler);
			var client:ClientLite = new ClientLite();
			client.id = AppSession.currentClient.id;
			AppGlobal.startWait();
			clientRequest.dispatch(client, ClientLiteRequestSignal.GET);
		}
	}
}